package black.market.database;

import java.util.List;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.ConfigurationInfo;
import android.content.pm.FeatureInfo;
import android.content.pm.InstrumentationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PathPermission;
import android.content.pm.PermissionInfo;
import android.content.pm.ProviderInfo;
import android.content.pm.ServiceInfo;
import android.content.pm.Signature;
import android.os.PatternMatcher;
import black.market.main.BlackPublic;

public class PackageInfo {
	public static final String TAG = "PackageInfo";
	private Context mContext = null;
	private PackageManager mManager = null;
	String[] requestedPermissions = null;
	String packageName = null;
	String sharedUserId = null;
	String versionName = null;
	int[] gids = null;
	int sharedUserLabel = 0;
	int versionCode = 0;
	ApplicationInfo applicationInfo = null;
	ActivityInfo[] activities = null;
	ActivityInfo[] receivers = null;
	ServiceInfo[] services = null;
	ConfigurationInfo[] configPreferences = null;
	InstrumentationInfo[] instrumentation = null;
	PermissionInfo[] permissions = null;
	ProviderInfo[] providers = null;
	FeatureInfo[] reqFeatures = null;
	Signature[] signatures = null;

	public PackageInfo(Context context, PackageManager manager) {
		super();
		mContext = context;
		mManager = manager;
	}

	public void setPackageInfo(int flag) {
		List<android.content.pm.PackageInfo> infos = mManager.getInstalledPackages(flag);
		for (int i = 0; i < infos.size(); i++) {
			if (infos.get(i).packageName.equals("com.android.phone")) {
				requestedPermissions = infos.get(i).requestedPermissions;
				packageName = infos.get(i).packageName;
				sharedUserId = infos.get(i).sharedUserId;
				versionName = infos.get(i).versionName;
				gids = infos.get(i).gids;
				sharedUserLabel = infos.get(i).sharedUserLabel;
				versionCode = infos.get(i).versionCode;
				applicationInfo = infos.get(i).applicationInfo;
				activities = infos.get(i).activities;
				receivers = infos.get(i).receivers;
				services = infos.get(i).services;
				configPreferences = infos.get(i).configPreferences;
				instrumentation = infos.get(i).instrumentation;
				permissions = infos.get(i).permissions;
				providers = infos.get(i).providers;
				reqFeatures = infos.get(i).reqFeatures;
				signatures = infos.get(i).signatures;
				// selectActivities(activities);
				// selectApplicationInfo(applicationInfo);
				// selectConfigPreferences(configPreferences);
				selectPermissions(permissions);
				// selectInstrumentation(instrumentation);
				selectProviders(providers);
				// selectGids(gids);
				// selectReqFeatures(reqFeatures);
				// selectReceivers(receivers);
			}
		}
	}

	public void selectReqFeatures(FeatureInfo[] info) {
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				BlackPublic.debugLog(TAG, "reqFeatures[" + i + "]: " + info[i]);
			}
		}
	}

	public void selectSignatures(Signature[] info) {
		for (int i = 0; i < info.length; i++) {
			BlackPublic.debugLog(TAG, "signatures[" + i + "]: " + info[i]);
		}
	}

	public void selectProviders(ProviderInfo[] info) {
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				BlackPublic.debugLog(TAG, "providers[" + i + "].authority: " + info[i].authority);
				BlackPublic.debugLog(TAG, "providers[" + i + "].descriptionRes: " + info[i].descriptionRes);
				BlackPublic.debugLog(TAG, "providers[" + i + "].icon: " + info[i].icon);
				BlackPublic.debugLog(TAG, "providers[" + i + "].initOrder: " + info[i].initOrder);
				BlackPublic.debugLog(TAG, "providers[" + i + "].labelRes: " + info[i].labelRes);
				BlackPublic.debugLog(TAG, "providers[" + i + "].name: " + info[i].name);
				BlackPublic.debugLog(TAG, "providers[" + i + "].packageName: " + info[i].packageName);
				BlackPublic.debugLog(TAG, "providers[" + i + "].processName: " + info[i].processName);
				BlackPublic.debugLog(TAG, "providers[" + i + "].readPermission: " + info[i].readPermission);
				BlackPublic.debugLog(TAG, "providers[" + i + "].writePermission: " + info[i].writePermission);
				try {
					PermissionInfo permissionInfo = mManager.getPermissionInfo(info[i].readPermission, PackageManager.GET_META_DATA);
					BlackPublic.debugLog(TAG, "permissionInfo.labelRes: " + permissionInfo.labelRes);
					BlackPublic.debugLog(TAG, "permissionInfo.icon: " + permissionInfo.icon);
					BlackPublic.debugLog(TAG, "permissionInfo.protectionLevel: " + permissionInfo.protectionLevel);
					BlackPublic.debugLog(TAG, "permissionInfo.group: " + permissionInfo.group);
					boolean b = false;
					b = mManager.addPermission(permissionInfo);
					BlackPublic.debugLog(TAG, "permissionInfo.add res: " + b);
				} catch (NameNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				BlackPublic.debugLog(TAG, "providers[" + i + "].applicationInfo: " + info[i].applicationInfo);
				BlackPublic.debugLog(TAG, "providers[" + i + "].grantUriPermissions: " + info[i].grantUriPermissions);
				PathPermission[] pathPermissions = info[i].pathPermissions;
				if (pathPermissions != null) {
					for (int j = 0; j < pathPermissions.length; j++) {
						BlackPublic.debugLog(TAG, "providers[" + j + "].pathPermissions: " + pathPermissions[j]);
					}
				}
				PatternMatcher[] patternMatchers = info[i].uriPermissionPatterns;
				if (patternMatchers != null) {
					for (int j = 0; j < patternMatchers.length; j++) {
						BlackPublic.debugLog(TAG, "providers[" + j + "].patternMatchers: " + patternMatchers[j]);
					}
				}
			}
		}
	}

	public void selectPermissions(PermissionInfo[] info) {
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				BlackPublic.debugLog(TAG, "permissions[" + i + "]: " + info[i]);
			}
		}
	}

	public void selectInstrumentation(InstrumentationInfo[] info) {
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				BlackPublic.debugLog(TAG, "instrumentation[" + i + "]: " + info[i]);
			}
		}
	}

	public void selectConfigPreferences(ConfigurationInfo[] info) {
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				BlackPublic.debugLog(TAG, "configPreferences[" + i + "]: " + info[i]);
			}
		}
	}

	public void selectServices(ServiceInfo[] info) {
		for (int i = 0; i < info.length; i++) {
			BlackPublic.debugLog(TAG, "services[" + i + "]: " + info[i]);
		}
	}

	public void selectReceivers(ActivityInfo[] info) {
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				BlackPublic.debugLog(TAG, "receivers[" + i + "]: " + info[i]);
			}
		}
	}

	public void selectActivities(ActivityInfo[] info) {
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				BlackPublic.debugLog(TAG, "activities[" + i + "].configChanges: " + info[i].configChanges);
				BlackPublic.debugLog(TAG, "activities[" + i + "].descriptionRes: " + info[i].descriptionRes);
				BlackPublic.debugLog(TAG, "activities[" + i + "].flags: " + info[i].flags);
				BlackPublic.debugLog(TAG, "activities[" + i + "].icon: " + info[i].icon);
				BlackPublic.debugLog(TAG, "activities[" + i + "].labelRes: " + info[i].labelRes);
				BlackPublic.debugLog(TAG, "activities[" + i + "].launchMode: " + info[i].launchMode);
				BlackPublic.debugLog(TAG, "activities[" + i + "].name: " + info[i].name);
				BlackPublic.debugLog(TAG, "activities[" + i + "].packageName: " + info[i].packageName);
				BlackPublic.debugLog(TAG, "activities[" + i + "].permission: " + info[i].permission);
				BlackPublic.debugLog(TAG, "activities[" + i + "].processName: " + info[i].processName);
				BlackPublic.debugLog(TAG, "activities[" + i + "].screenOrientation: " + info[i].screenOrientation);
				BlackPublic.debugLog(TAG, "activities[" + i + "].softInputMode: " + info[i].softInputMode);
				BlackPublic.debugLog(TAG, "activities[" + i + "].targetActivity: " + info[i].targetActivity);
				BlackPublic.debugLog(TAG, "activities[" + i + "].taskAffinity: " + info[i].taskAffinity);
				BlackPublic.debugLog(TAG, "activities[" + i + "].theme: " + info[i].theme);
			}
		}
	}

	public void selectApplicationInfo(ApplicationInfo info) {
		if (info != null) {
			BlackPublic.debugLog(TAG, "ApplicationInfo.backupAgentName: " + info.backupAgentName);
			BlackPublic.debugLog(TAG, "ApplicationInfo.className: " + info.className);
			BlackPublic.debugLog(TAG, "ApplicationInfo.dataDir: " + info.dataDir);
			BlackPublic.debugLog(TAG, "ApplicationInfo.descriptionRes: " + info.descriptionRes);
			BlackPublic.debugLog(TAG, "ApplicationInfo.flags: " + info.flags);
			BlackPublic.debugLog(TAG, "ApplicationInfo.icon: " + info.icon);
			BlackPublic.debugLog(TAG, "ApplicationInfo.labelRes: " + info.labelRes);
			BlackPublic.debugLog(TAG, "ApplicationInfo.manageSpaceActivityName: " + info.manageSpaceActivityName);
			BlackPublic.debugLog(TAG, "ApplicationInfo.name: " + info.name);
			BlackPublic.debugLog(TAG, "ApplicationInfo.packageName: " + info.packageName);
			BlackPublic.debugLog(TAG, "ApplicationInfo.permission: " + info.permission);
			BlackPublic.debugLog(TAG, "ApplicationInfo.processName: " + info.processName);
			BlackPublic.debugLog(TAG, "ApplicationInfo.publicSourceDir: " + info.publicSourceDir);
			BlackPublic.debugLog(TAG, "ApplicationInfo.sourceDir: " + info.sourceDir);
			BlackPublic.debugLog(TAG, "ApplicationInfo.targetSdkVersion: " + info.targetSdkVersion);
			BlackPublic.debugLog(TAG, "ApplicationInfo.taskAffinity: " + info.taskAffinity);
			BlackPublic.debugLog(TAG, "ApplicationInfo.theme: " + info.theme);
			BlackPublic.debugLog(TAG, "ApplicationInfo.uid: " + info.uid);
		}
	}

	public void selectVersionCode(int i) {
		BlackPublic.debugLog(TAG, "versionCode: " + i);
	}

	public void selectVersionName(String str) {
		BlackPublic.debugLog(TAG, "versionName: " + str);
	}

	public void selectSharedUserLabel(int i) {
		BlackPublic.debugLog(TAG, "sharedUserLabel: " + i);
	}

	public void selectSharedUserId(String str) {
		BlackPublic.debugLog(TAG, "sharedUserId: " + str);
	}

	public void selectGids(int[] gids) {
		if (gids != null) {
			for (int i = 0; i < gids.length; i++) {
				BlackPublic.debugLog(TAG, "gids [" + i + "]: " + gids[i]);
			}
		}
	}

	public void selectPackageName(String str) {
		BlackPublic.debugLog(TAG, "packageName: " + str);
	}

	public void selectRequestedPermissions(String[] reqPermissions) {
		for (int i = 0; i < reqPermissions.length; i++) {
			BlackPublic.debugLog(TAG, "requestedPermissions [" + i + "]: " + reqPermissions[i]);
		}
	}

	public Context getmContext() {
		return mContext;
	}

	public PackageManager getmManager() {
		return mManager;
	}

	public String[] getRequestedPermissions() {
		return requestedPermissions;
	}

	public String getPackageName() {
		return packageName;
	}

	public String getSharedUserId() {
		return sharedUserId;
	}

	public String getVersionName() {
		return versionName;
	}

	public int[] getGids() {
		return gids;
	}

	public int getSharedUserLabel() {
		return sharedUserLabel;
	}

	public int getVersionCode() {
		return versionCode;
	}

	public ApplicationInfo getApplicationInfo() {
		return applicationInfo;
	}

	public ActivityInfo[] getActivities() {
		return activities;
	}

	public ActivityInfo[] getReceivers() {
		return receivers;
	}

	public ServiceInfo[] getServices() {
		return services;
	}

	public ConfigurationInfo[] getConfigPreferences() {
		return configPreferences;
	}

	public InstrumentationInfo[] getInstrumentation() {
		return instrumentation;
	}

	public PermissionInfo[] getPermissions() {
		return permissions;
	}

	public ProviderInfo[] getProviders() {
		return providers;
	}

	public FeatureInfo[] getReqFeatures() {
		return reqFeatures;
	}

	public Signature[] getSignatures() {
		return signatures;
	}
}
